<?php
/* @var $this SelisteController */
/* @var $model Seliste */
/* @var $form CActiveForm */
$cs = Yii::app()->getClientScript();
$cs->registerScript('form_grains', "
	// Enable the Tabs
	$('#myTab a.atab').click(function (e) {
		e.preventDefault();
		$(this).tab('show');
	});
	function addOkCheck () {
		$('.checkOk').remove();
		$('form input:checked').after('<span class=\"glyphicon glyphicon-ok checkOk\"></span>');
	}	
	function register_change_check() {
		$('form input[type=radio], form input[type=checkbox]').change(function() {
			//$('.checkgrain').change(function() {
			addOkCheck();
			if ($(this).hasClass('checkgrain')) {
				if($(this).is(\":checked\")) {
					$('#recap_grains').append('<button type=\"button\" class=\"btn btn-xs btn-info\" data-type=\"'+$(this).attr(\"data-type\")+'\" data-domaine=\"'+$(this).attr(\"data-domaine\")+'\" data-grain=\"'+$(this).attr(\"data-grain\")+'\" id=\"btn_'+$(this).attr(\"id\")+'\"><span class=\"glyphicon glyphicon-remove\"></span>'+$.trim($('#myTab li.active')[0].innerText)+' / '+$(this).next().next().text()+'</button>');
					register_recap_btn();
				} else {
					$('#btn_'+$(this).attr(\"id\")).remove();
				}
			}
		});
	}
	function register_recap_btn() {
		$('.btn-info').click(function() {
			$('#checkgrain_'+$(this).attr('data-grain')).attr('checked', false);
			$('#checkgrain_'+$(this).attr('data-grain')).parent().removeClass('active');
			$('#checkgrain_'+$(this).attr('data-grain')).next('.checkOk').remove();
			$(this).remove();
		});
	}
	$('.sendgrain').click(function() {
		var data=$('#grain-form').serialize();
		$.ajax({
			type: 'POST',
			url: '" . Yii::app()->createAbsoluteUrl("grain/create") . "',
			data:data,
			success:function(data){
				$('#myModal').modal('hide');
				// on recharge la liste des checkbox
				$.ajax({
					type: 'POST',
					url: '" . Yii::app()->createAbsoluteUrl("seliste/tab_grains",array('id'=>$model->id)) . "',
					cache:false,
					data: {domaine:data.domaine,type:data.type},
					success:function(datarep){
						$('#choix_'+datarep.type+'_'+datarep.domaine).html(datarep.html);
						// on vérifie si un des éléments de la liste était coché / décoché avant l'ajout en parcourant la liste
						$('.btn-info').each(function() {
							if (($(this).attr('data-type') == datarep.type) && ($(this).attr('data-domaine') == datarep.domaine)) {
								$('#checkgrain_'+$(this).attr('data-grain')).attr('checked', true);
								$('#checkgrain_'+$(this).attr('data-grain')).parent().addClass('active');
							}
						});
						register_change_check();
						register_recap_btn();
						addOkCheck();
					},
					dataType:'json'
				});
			},
			error: function(data) {
			},
			dataType:'json'
		});
	});
	register_change_check();
	register_recap_btn();
	$('.btn-addgrain').click(function(){
		$('#myModalLabel').text($(this).text());
		$('#Grain_idtype').val($(this).attr('data-type'));
		$('#Grain_iddomaine').val($(this).attr('data-domaine'));
		$('#Grain_nom').val('');
		$('#Grain_descr').val('');
		$('#Grain_valeur').val('');
		$('#Grain_unite').val('');
		$('#myModal').modal()
	});
	addOkCheck();
	$('.unvalidate').tooltip();
",CClientScript::POS_READY );
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'grains-form',
	'htmlOptions'=>array('class'=>'form-horizontal'),
	'enableAjaxValidation'=>false,
)); ?>

	<?php echo $form->errorSummary($model,NULL,NULL,array("class"=>"alert alert-danger")); ?>

	<?php // navbar type/domaine ?>
	<ul class="nav nav-tabs" id="myTab">
	<?php
	$types = Type::model()->findAll(array('order'=>'lib asc'));
	$domaines = Domaine::model()->findAll(array('order'=>'lib asc'));
	$i=0;
	foreach ($types as $type) {
		?>
		<li class="dropdown<?php echo (($i==0) ? " active" : "");?>">
			<a class="dropdown-toggle" data-toggle="dropdown" href="#">
				<?php echo $type->lib;?> <span class="caret"></span>
			</a>
			<ul class="dropdown-menu">
			<?php
			foreach ($domaines as $domaine) {
				// on regarde s'il y a des grains dans ce type/domaine
				//$grains = Grain::model()->findAll("idtype=" . $type->id . " AND iddomaine=" . $domaine->id . " AND (status=1 or idseliste=" . Yii::app()->user->id . ")",array("order"=>"nom asc"));
				$grains = Grain::model()->findAll("idtype=" . $type->id . " AND iddomaine=" . $domaine->id,array("order"=>"nom asc"));
				$g=0;
				foreach ($grains as $grain) {
					// on regarde si l'utilisateur courant a le droit de visualiser le grain
					if ($grain->status==1 || $grain->idseliste==Yii::app()->user->id || Yii::app()->user->admin_grain) $g++;
				}
				$ast = "";
				if ($g>0) $ast = "<span class=\"badge pull-right\">" . $g . "</span>";
				$i++;
				?>
				<li<?php echo (($i==1) ? " class=\"active\"" : "");?>><a class="atab" href="#onglet<?php echo $type->id . $domaine->id;?>" data-toggle="tab"><?php echo $ast . $domaine->lib;?></a></li>
				<?php
			}
			?>
			</ul>
		</li>
		<?php
	}
	?>
	</ul>

	<?php // onglets type/domaine ?>
	<div class="tab-content panel panel-default" id="liste_grains">
	<?php
	$grains_seliste = array();
	foreach ($model->grains as $grain) {
		$grains_seliste[] = $grain->id;
	}
	$i=0;
	foreach ($types as $type) {
		foreach ($domaines as $domaine) {
			$i++;
			echo "<div id=\"onglet" . $type->id . $domaine->id . "\" class=\"tab-pane fade" . (($i==1) ? " in active" : "") . "\">";
				?>
				<ol class="breadcrumb">
					<li><?php echo $type->lib;?></li>
					<li><?php echo $domaine->lib;?></li>
				</ol>
				
				<div class="form-group" id="choix_<?php echo $type->id;?>_<?php echo $domaine->id;?>">
				<?php	$this->renderPartial('_tab_grains',array('grains_seliste'=>$grains_seliste,'domaine'=>$domaine->id,'type'=>$type->id));?>
				</div>
				
				<div class="form-group" id="addgrain<?php echo $domaine->id;?>">
					<div class="col-lg-10 col-lg-offset-1">
						<?php
						echo CHtml::htmlButton(
								"<span class=\"glyphicon glyphicon-plus\"></span> Ajouter un grain dans " . $type->lib . " / " . $domaine->lib,
								array(	
										'id'=>"btn-addgrain-" . $domaine->id,
										'class'=>"btn btn-primary btn-sm btn-addgrain",
										'data-type'=>$type->id,
										'data-domaine'=>$domaine->id,
								)
						);
						?>
					</div>
				</div>
				
			</div>
			<?php
		}
	}
	?>
	</div>
			
	<div class="panel panel-default">
		<div class="panel-heading">
			<h3 class="panel-title">Liste des grains sélectionnés</h3>
		</div>
		<div class="panel-body" id="recap_grains">
			<?php
			foreach ($model->grains as $grain) {
				if ($grain->status==1 || $grain->idseliste==Yii::app()->user->id || Yii::app()->user->admin_grain) {
					$type = Type::model()->findByPk($grain->idtype);
					echo "<button type=\"button\" class=\"btn btn-xs btn-info\" data-type=\"" . $grain->idtype . "\" data-domaine=\"" . $grain->iddomaine . "\" data-grain=\"" . $grain->id . "\" id=\"btn_checkgrain_" . $grain->id . "\"><span class=\"glyphicon glyphicon-remove\"></span>" . $type->lib . " / " . $grain->nom . "</button>";
				}
			}
			?>
		</div>
	</div>

	<div class="form-group">
		<div class="col-lg-offset-1 col-lg-10">
			<?php if ($compte) {?>
				<?php echo CHtml::submitButton('Modifier la liste de mes grains',array("class"=>"btn btn-primary")); ?>
				<?php echo CHtml::link("Annuler la modification",array('seliste/compte'),array('class'=>"btn btn-sm btn-info"));	?>
			<?php } else {?>
				<?php echo CHtml::submitButton('Valider la liste des grains du séliste',array("class"=>"btn btn-primary")); ?>
				<?php echo CHtml::link("Annuler les modifications",array('seliste/admin'),array('class'=>"btn btn-sm btn-info"));	?>
			<?php }?>
		</div>
	</div>

<?php $this->endWidget(); ?>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove"></span></button>
				<h4 class="modal-title" id="myModalLabel"></h4>
			</div>
			<div class="modal-body">
				<?php $modelgrain = new Grain;?>
				<?php $this->renderPartial('/grain/_form', array('model'=>$modelgrain,'modal'=>'myModal')); ?>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Annuler</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

</div><!-- form -->